﻿using MessageBoxNS;
using System;
using System.Collections;

using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text.RegularExpressions;
using UnityEngine;

using UnityEngine.UI;



public class UIController : MonoBehaviour
{
    /// <summary>
    /// 选择文件控制
    /// </summary>
    public SelectFile m_selectFile;

    /// <summary>
    /// 选择表名及切割方式控制
    /// </summary>
    public SelectInciseType m_selectInciseType;

    /// <summary>
    /// 选择行列控制
    /// </summary>
    public ColumnSetting m_columnSetting;

    /// <summary>
    /// 显示面板控制
    /// </summary>
    public ShowTextScript m_showTextScript;

    /// <summary>
    /// 版本
    /// </summary>
    public EVersions SelectFileVersions { get; private set; }
    private string selectPath;
    /// <summary>
    /// 选择路径
    /// </summary>
    public string SelectPath
    {
        get
        {
            return selectPath;
        }
        set
        {
            selectPath = value;
            if (!string.IsNullOrEmpty(selectPath))
            {
                string ext = Path.GetExtension(selectPath);
                SelectFileVersions = ext == ".xls" ? EVersions.A1997To2003 : EVersions.A2007;

            }
        }
    }


    /// <summary>
    /// 切割方式
    /// </summary>
    public EIncise ESelectInciseType { get; set; }

    /// <summary>
    /// 选择表名
    /// </summary>
    public string SelectTabName { get; set; }

    /// <summary>
    /// 切割列数
    /// </summary>
    public int? InciseColumn { get; set; }

    public int? ProvinceIndex = null;
    public int? CityIndex = null;
    public int? CountyIndex = null;

    /// <summary>
    /// 表格总数据
    /// </summary>
    public Dictionary<string, ExcelData> ExcelData = new Dictionary<string, ExcelData>();


    void Start()
    {
        ESelectInciseType = EIncise.智能切割;

    }
    private void Update()
    {
        //if (Input.GetKeyDown(KeyCode.A))
        //{
        //    GetNewDataDic();
        //}
    }

    /// <summary>
    /// 设置表数据
    /// </summary>
    /// <param name="fileName"></param>
    public void SetData(string fileName)
    {
        ExcelData.Clear();
        ExcelData = DoExcel.GetExcelData(fileName, SelectFileVersions);
    }

    /// <summary>
    /// 获取表数据
    /// </summary>
    /// <returns></returns>
    public Dictionary<string, ExcelData> GetData()
    {
        return ExcelData;
    }

    /// <summary>
    /// 刷新列数据
    /// </summary>
    /// <param name="path"></param>
    public void ReFreshSHowText(string path = null)
    {
        if (path == null)
        {
            path = SelectPath;
        }
        ExcelData excelData = ExcelData[SelectTabName];
        m_showTextScript.targetText.text = "";
        int tempColumn = InciseColumn == null ? 0 : (int)InciseColumn;
        if (tempColumn >= excelData.ExcelContent[0].Count - 1)
        {
            MessageBox.ShowConfirmMessageBox("MessageBox", "输入列数大于总列数", "知道了");
        }
        for (int i = 0; i < excelData.ExcelContent.Count; i++)
        {
            m_showTextScript.targetText.text += (excelData.ExcelContent[i][tempColumn] + "\n");
        }
    }

    public Dictionary<string, ExcelData> GetNewDataDic()
    {
        Dictionary<string, ExcelData> NewExcelData = DeepCopy(ExcelData);
        ExcelData excelData = NewExcelData[SelectTabName];
        int tempColumn = InciseColumn == null ? 0 : (int)InciseColumn;
        //这里没有判断如果没赋值情况,所以采用异常捕捉
        for (int i = 1; i < excelData.ExcelContent.Count; i++)
        {
            SplitInfo split = Incise(excelData.ExcelContent[i][tempColumn]);
            if (ProvinceIndex != null)
                excelData.ExcelContent[i][(int)ProvinceIndex] = split.Province;
            if (CityIndex != null)
                excelData.ExcelContent[i][(int)CityIndex] = split.City;
            if (CountyIndex != null)
                excelData.ExcelContent[i][(int)CountyIndex] = split.County;
            excelData.ExcelContent[i][tempColumn] = split.Village;
        }

        return NewExcelData;
    }

    /// <summary>
    /// 判断输入赋值列是否越界
    /// </summary>
    /// <returns></returns>
    public bool IsColumnAnArray()
    {
        ExcelData excelData = ExcelData[SelectTabName];
        int sumCount = excelData.ExcelContent[0].Count - 1;

        if (AnArray(sumCount, ProvinceIndex)) return true;
        if (AnArray(sumCount, CityIndex)) return true;
        if (AnArray(sumCount, CountyIndex)) return true;

        return false;
    }

    public bool AnArray(int sumCount, int? currentCount)
    {
        int tempColumn = currentCount == null ? 0 : (int)currentCount;
        if (tempColumn >= sumCount)
        {
            return true;
        }
        return false;
    }

    public SplitInfo Incise(string str)
    {
        SplitInfo split;
        switch (ESelectInciseType)
        {
            case EIncise.智能切割:
                split = MatchSelf.SplitStrInfo(str);
                break;
            case EIncise.特殊字符:
                string[] strGroup = str.Split(new char[] { '*' }, 4);
                split = new SplitInfo(strGroup[0], strGroup[1], strGroup[2], strGroup[3]);
                break;
            default:
                split = new SplitInfo("", "", "", "");
                break;
        }
        return split;
    }

    public T DeepCopy<T>(T obj)
    {
        object retval;
        using (MemoryStream memoryStream = new MemoryStream())
        {
            BinaryFormatter formatter = new BinaryFormatter();

            formatter.Serialize(memoryStream, obj);

            memoryStream.Seek(0, SeekOrigin.Begin);
            retval = formatter.Deserialize(memoryStream);
            memoryStream.Close();
        }
        return (T)retval;
    }
}
[System.Serializable]
public class ExcelData
{
    public List<List<string>> ExcelContent;
    public ExcelData()
    {
        ExcelContent = new List<List<string>>();
    }
}